package pers.cj.pdos.interceptor;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import pers.cj.pdos.utils.ClientInfo;
import pers.cj.pdos.utils.EncryptDES3;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Base64;

public class CustomFormAuthenticationFilter extends FormAuthenticationFilter {

    /**
     * 表示当访问拒绝时
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        String sessionValidateCode = (String) session.getAttribute("validateCode");

        String validateCode = request.getParameter("validateCode");

        if (sessionValidateCode!=null && validateCode!=null && !validateCode.equals(session.getAttribute("validateCode"))) {
            //如果校验失败，将验证码错误失败信息，通过shiroLoginFailure设置到request中
            request.setAttribute("shiroLoginFailure","randomCodeError");
            return true;
        }

        return super.onAccessDenied(request, response);
    }



    /**
     * 当登录成功
     * @param token
     * @param subject
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        UsernamePasswordToken userToken = (UsernamePasswordToken)token;
        addCookie(userToken.getUsername(),new String(userToken.getPassword()),response,request);

        response.sendRedirect(ClientInfo.getBaseUrl(request)+"/");
        return false;
    }



    private void addCookie(String loginAccount,String loginPassword, HttpServletResponse response,HttpServletRequest request) throws Exception {
        if(!loginAccount.equals("") && !loginPassword.equals("")){
            //创建  Cookie
            Cookie loginAccountCookie = new Cookie("loginAccount",loginAccount);

            EncryptDES3 des = new EncryptDES3();
            String encryptPass = Base64.getEncoder().encodeToString(des.Encrytor(loginPassword));
            Cookie loginPasswordCookie = new Cookie("loginPassword", encryptPass);// 密码使用加密存储
            //设置Cookie的父路经
            loginAccountCookie.setPath(request.getContextPath()+"/");
            loginPasswordCookie.setPath(request.getContextPath()+"/");

            //保存Cookie的时间长度，单位为秒
            loginAccountCookie.setMaxAge(7*24*60*60);
            loginPasswordCookie.setMaxAge(7*24*60*60);

            //加入Cookie到响应头
            response.addCookie(loginAccountCookie);
            //response.addCookie(loginPasswordCookie);
        }
    }
}
